package com.ruoyi.web.controller.deviceManager;

import com.cs.deviceManager.domain.DevDevice;
import com.cs.deviceManager.mqtt.MqttUtil;
import com.cs.deviceManager.service.IDevDeviceService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import jakarta.annotation.Resource;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 设备信息Controller
 * 
 * @author zizige
 * @date 2025-06-30
 */
@Controller
@RequestMapping("/deviceManager/device")
public class DevDeviceController extends BaseController
{
    private String prefix = "deviceManager/device";

    @Resource
    private IDevDeviceService devDeviceService;
    
    @Value("${mqtt.client-id:device-status-listener}")
    private String clientId;

    @RequiresPermissions("deviceManager:device:view")
    @GetMapping()
    public String device()
    {
        return prefix + "/device";
    }


    /**
     * 查询设备信息列表
     */
    @RequiresPermissions("deviceManager:device:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(DevDevice devDevice)
    {
        startPage();
        List<DevDevice> list = devDeviceService.selectDevDeviceList(devDevice);
        return getDataTable(list);
    }

    /**
     * 导出设备信息列表
     */
    @RequiresPermissions("deviceManager:device:export")
    @Log(title = "设备信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(DevDevice devDevice)
    {
        List<DevDevice> list = devDeviceService.selectDevDeviceList(devDevice);
        ExcelUtil<DevDevice> util = new ExcelUtil<DevDevice>(DevDevice.class);
        return util.exportExcel(list, "设备信息数据");
    }

    /**
     * 新增设备信息
     */
    @RequiresPermissions("deviceManager:device:add")
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存设备信息
     */
    @RequiresPermissions("deviceManager:device:add")
    @Log(title = "设备信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(DevDevice devDevice)
    {
        return toAjax(devDeviceService.insertDevDevice(devDevice));
    }

    /**
     * 修改设备信息
     */
    @RequiresPermissions("deviceManager:device:edit")
    @GetMapping("/edit/{devId}")
    public String edit(@PathVariable("devId") Long devId, ModelMap mmap)
    {
        DevDevice devDevice = devDeviceService.selectDevDeviceByDevId(devId);
        mmap.put("devDevice", devDevice);
        return prefix + "/edit";
    }

    /**
     * 修改保存设备信息
     */
    @RequiresPermissions("deviceManager:device:edit")
    @Log(title = "设备信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(DevDevice devDevice)
    {
        return toAjax(devDeviceService.updateDevDevice(devDevice));
    }

    /**
     * 删除设备信息
     */
    @RequiresPermissions("deviceManager:device:remove")
    @Log(title = "设备信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(devDeviceService.deleteDevDeviceByDevIds(ids));
    }
    
    /**
     * 发布MQTT消息
     */
    @RequiresPermissions("deviceManager:device:edit")
    @Log(title = "设备信息", businessType = BusinessType.UPDATE)
    @PostMapping("/publish")
    @ResponseBody
    public AjaxResult publishMqttMessage(@RequestParam String topic, 
                                         @RequestParam String message) {
        boolean success = MqttUtil.publish(clientId, topic, message);
        if (success) {
            return AjaxResult.success("消息发布成功");
        } else {
            return AjaxResult.error("消息发布失败");
        }
    }
    
    /**
     * 获取可用设备列表
     */
    @GetMapping("/activeList")
    @ResponseBody
    public AjaxResult getActiveDeviceList() {
        List<DevDevice> deviceList = devDeviceService.selectActiveDevices();
        return AjaxResult.success(deviceList);
    }
}